using System;
using System.Collections;
using System.Text.RegularExpressions;
using SecuBat.Common;

namespace SecuBat.Analysis.Common
{
    /// <summary>
    /// Strongly typed collection of AWSAF.Web.Analysis.WeightedKeyword.
    /// </summary>
    public class WeightedKeywordCollection : SharedData, IDictionary, IEnumerable, ICollection
    {
			private Hashtable _table;
			private Regex _regex;

			public WeightedKeywordCollection()
			{
				_table = new Hashtable();
			}

			public void BuildRegex()
			{
				String keywords = "";
				foreach (WeightedKeyword keyword in this._table.Values)
					keywords += keyword.Keyword + "|";

				if (keywords.Length > 0)
					keywords = keywords.Substring(0, keywords.Length - 1);

				keywords = keywords.Replace(" ", "\\s");
				_regex = new Regex(keywords, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
			}

			public void CopyTo(Array array, int index)
			{
				ReadLock();
				_table.CopyTo(array, index);
				ReadRelease();
			}

			public int Count
			{
				get
				{
					ReadLock();
					int c = _table.Count;
					ReadRelease();
					return c;
				}
			}

			public object SyncRoot
			{
				get
				{
					ReadLock();
					object o = _table.SyncRoot;
					ReadRelease();
					return o;
				}
			}

			public bool IsSynchronized
			{
				get
				{
					ReadLock();
					bool s = _table.IsSynchronized;
					ReadRelease();
					return s;
				}
			}

			public bool Contains(WeightedKeyword keyword)
			{
				ReadLock();
				bool b = _table.Contains(keyword.Keyword.ToLower());
				ReadRelease();
				return b;
			}

			public bool Contains(object key)
			{
				ReadLock();
				bool b = _table.Contains(key);
				ReadRelease();
				return b;
			}

			public void Add(object key, object value)
			{
				WriteLock();
				if (!_table.Contains(key))
					_table.Add(key, value);
				WriteRelease();
			}

			public void Add(WeightedKeyword keyword)
			{
				WriteLock();
				if (keyword != null)
					this.Add(keyword.Keyword.ToLower(), keyword);
				WriteRelease();
			}

			public void Clear()
			{
				WriteLock();
				_table.Clear();
				WriteRelease();
			}

			IDictionaryEnumerator System.Collections.IDictionary.GetEnumerator()
			{
				ReadLock();
				IDictionaryEnumerator e = _table.GetEnumerator();
				ReadRelease();
				return e;
			}

			IEnumerator System.Collections.IEnumerable.GetEnumerator()
			{
				ReadLock();
				IEnumerator e = _table.GetEnumerator();
				ReadRelease();
				return e;
			}

			public void Remove(object key)
			{
				WriteLock();
				_table.Remove(key);
				WriteRelease();
			}

			public ICollection Keys
			{
				get
				{
					ReadLock();
					ICollection k = _table.Keys;
					ReadRelease();
					return k;
				}
			}

			public ICollection Values
			{
				get
				{
					ReadLock();
					ICollection v = _table.Values;
					ReadRelease();
					return v;
				}
			}

			public bool IsReadOnly
			{
				get
				{
					ReadLock();
					bool i = _table.IsReadOnly;
					ReadRelease();
					return i;
				}
			}

			public bool IsFixedSize
			{
				get
				{
					ReadLock();
					bool f = _table.IsFixedSize;
					ReadRelease();
					return f;
				}
			}

    	public Regex KeywordRegex
    	{
    		get
    		{
					if (_regex == null)
						this.BuildRegex();
    			return _regex;
    		}
    	}

    	public object this[object key]
			{
				get
				{
					ReadLock();
					object o = _table[key];
					ReadRelease();
					return o;
				}
				set
				{
					WriteLock();
					_table[key] = value;
					WriteRelease();
				}
			}
		}
}
